<template>
  <view class="search">
    <view class="searchBar">
      <uni-search-bar @confirm="search" @input="input" cancelButton="none" :radius="36">
      </uni-search-bar>
    </view>
    <!-- 搜索列表 -->
    <view class="uniList" v-if="searchList.length !== 0">
      <view class="list_item" v-for="(item,index) in searchList" :key="index" @click="gotoDetail(item)">
        <text class="goods_name">{{item.goods_name}}</text>
        <uni-icons type="forward" size="16"></uni-icons>
      </view>
    </view>
    <!-- 搜索历史 -->
    <view class="histrory_list" v-else>
      <view class="history_header">
        <text>搜索历史</text>
        <uni-icons type="trash-filled" size="17" @click="clearAll"></uni-icons>
      </view>
      <!-- 列表区域 -->
      <view class="history_content">
        <uni-tag :text="item" v-for="(item,i) in historyListSet" :key="i" @click="gotoGoodsList(item)"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  import {
    qSearch
  } from '@/http/api.js'
  export default {
    data() {
      return {
        // 输入框防抖
        timer: null,
        // 关键字
        kw: '',
        searchList: [],
        // 保存搜索历史记录
        historyList: [],
        // 去重后搜索历史记录
        historyListSet: []
      };
    },
    onLoad() {
      // 获取本地搜索的关键词
      this.historyListSet = JSON.parse(uni.getStorageSync('kw') || '[]')
    },
    methods: {
      // 输入事件
      input(e) {
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          this.kw = e
          this.getQSearch()
        }, 500)
      },

      // 获取搜索建议列表
      async getQSearch() {
        if (this.kw.trim() === '') {
          this.searchList = []
          return
        }
        const {
          data: res
        } = await qSearch({
          query: this.kw
        })
        if (res.meta.status !== 200) return uni.$showMsg()
        this.searchList = res.message
        this.historySet()
      },
      // 搜索关键词去重
      historySet() {
        this.historyList.push(this.kw)
        const arrSet = new Set(this.historyList)
        arrSet.delete(this.kw)
        arrSet.add(this.kw)
        this.historyList = Array.from(arrSet)
        this.historyListSet = [...this.historyList].reverse()
        // 将搜索记录存储到本地
        uni.setStorageSync('kw', JSON.stringify(this.historyListSet))
        console.log(this.historyListSet)
      },
      // 点击跳转到商品详情页
      gotoDetail(item) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      },
      // 清除全部搜索记录
      clearAll() {
        this.historyList = []
        uni.removeStorageSync('kw')
        this.historySet()
      },
      // 点击搜索历史,跳转到商品列表页
      gotoGoodsList(item) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?query=' + item
        })
      }
    }
  }
</script>

<style lang="scss">
  .searchBar {
    position: sticky;
    top: 0;
    z-index: 999;
    height: 100rpx;
    padding: 0 30rpx;
    background-color: #c00000;
  }

  .uniList {
    padding: 0 10rpx;

    .list_item {
      display: flex;
      align-items: center;
      font-size: 24rpx;
      padding: 26rpx 0;
      border-bottom: 2rpx solid #efefef;
      justify-content: space-between;

      .goods_name {
        display: block;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
        margin-right: 6rpx;
      }
    }
  }

  .histrory_list {
    padding: 0rpx 10rpx;

    .history_header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 80rpx;
      font-size: 26rpx;
      border-bottom: 2rpx solid #efefef;
    }

    .history_content {
      display: flex;
      flex-wrap: wrap;
      margin-top: 20rpx;

      .uni-tag {
        margin-top: 10rpx;
        margin-right: 10rpx;
      }
    }
  }
</style>
